package com.dmap.knowledge.service.impl;

import com.dmap.knowledge.dao.BannerMapper;
import com.dmap.knowledge.po.Article;
import com.dmap.knowledge.po.Banner;
import com.dmap.knowledge.po.BannerExample;
import com.dmap.knowledge.po.BannerTag;
import com.dmap.knowledge.service.ArticleService;
import com.dmap.knowledge.service.BannerExtensionService;
import com.dmap.knowledge.service.BannerService;
import com.dmap.knowledge.service.BannerTagService;
import com.dmap.knowledge.vo.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author zmh
 * @date 18-7-18 下午7:09
 */
@Service
public class BannerExtensionServiceImpl implements BannerExtensionService {
    @Resource
    private BannerMapper bannerMapper;
    @Resource
    private BannerTagService bannerTagService;
    @Resource
    private ArticleService articleService;

    @Resource
    private BannerService bannerService;
    
    @Override
    public int countBanner() {
        BannerExample bannerExample = new BannerExample();
        return bannerMapper.countByExample(bannerExample);
    }
    
    @Override
    public BannerInfoVo selectBannerList(String appNumber, String lang, String companyNumber, String key, int pageIndex, int pageSize) {
        BannerExample bannerExample = new BannerExample();
        bannerExample.setOrderByClause("sort asc,update_time desc");
        BannerExample.Criteria criteria = bannerExample.createCriteria();
        criteria.andLangEqualTo(lang);
        if (appNumber != null) {
            criteria.andAppNumberEqualTo(appNumber);
        }
        if (companyNumber != null) {
            criteria.andCompanyNumberEqualTo(companyNumber);
        }
        if (key != null) {
            criteria.andTitleLike("%" + key + "%");
        }
        bannerExample.setLimitStart((pageIndex - 1) * pageSize);
        bannerExample.setLimitEnd(pageSize);
        List<Banner> banners = bannerMapper.selectByExample(bannerExample);
        BannerInfoVo bannerInfoVo = new BannerInfoVo();
        List<BannerVo> bannerVoList = new ArrayList<>();
        banners.forEach(banner -> {
            BannerVo bannerVo = new BannerVo(banner);
            List<BannerTag> bannerTagList = bannerTagService.selectByBannerId(banner.getId());
            //增加标签
            List<BannerTagVo> bannerTagVoList = new ArrayList<>();
            bannerTagList.forEach(bannerTag->{
                BannerTagVo bannerTagVo = new BannerTagVo(bannerTag);
                bannerTagVoList.add(bannerTagVo);
            });
            bannerVo.setTags(bannerTagVoList);
            bannerVo.setSchedule(bannerService.bannerScheduleInfo(banner));
            bannerVoList.add(bannerVo);
        });
        bannerInfoVo.setBannerList(bannerVoList);
        bannerInfoVo.setCount(countSelectBanner(appNumber, lang, companyNumber, key));
        return bannerInfoVo;
    }
    

    @Override
    public List<AppBannerVo> selectAppBanner(String lang, String projectId) {
        BannerExample bannerExample = new BannerExample();
        //筛选app banner的前五条
        bannerExample.setLimitStart(0);
        bannerExample.setLimitEnd(5);
        //按照推荐索引降序  创建日期降序
        bannerExample.setOrderByClause("sort asc,update_time desc");
        BannerExample.Criteria criteria = bannerExample.createCriteria();
        //选择语言
        criteria.andLangEqualTo(lang.toUpperCase());
        //app的名称
        criteria.andAppNumberEqualTo(projectId.toUpperCase());
        //结束时间大于现在时间
        criteria.andEndTimeGreaterThan(System.currentTimeMillis() / 1000);
        //开始时间小于现在时间
        criteria.andStartTimeLessThanOrEqualTo(System.currentTimeMillis() / 1000);
        //banner的为已上架
        criteria.andStatusEqualTo(1);
        List<Banner> appBannerList = bannerMapper.selectByExample(bannerExample);
        List<AppBannerVo> appBannerVos = new ArrayList<>();
        for (Banner banner : appBannerList) {
            AppBannerVo appBannerVo = new AppBannerVo(banner);
            long id = banner.getId();
            List<BannerTag> bannerTags = bannerTagService.selectByBannerId(id);
            bannerTags.forEach(bannerTag -> appBannerVo.getTags().add(bannerTag.getTagName()));
            //根据jumpid 跳转页面
            if (banner.getJumpId() != null) {
                Article article = articleService.selectByArticleId(banner.getJumpId());
                String articleSummary = article != null ? article.getSummary() : "";
                appBannerVo.setSummary(articleSummary);
            }
            appBannerVos.add(appBannerVo);
        }
        return appBannerVos;
    }
    
    @Override
    public int countSelectBanner(String appNumber, String lang, String companyNumber, String key) {
        BannerExample bannerExample = new BannerExample();
        bannerExample.setOrderByClause("create_time desc");
        BannerExample.Criteria criteria = bannerExample.createCriteria();
        criteria.andLangEqualTo(lang);
        if (appNumber != null) {
            criteria.andAppNumberEqualTo(appNumber);
        }
        if (key != null) {
            criteria.andTitleLike("%" + key + "%");
        }
        if (companyNumber != null) {
            criteria.andCompanyNumberEqualTo(companyNumber);
        }
        return bannerMapper.countByExample(bannerExample);
    }
    
    @Override
    public BannerInfoOneVo selectBannerInfo(String bannerId) {
        Banner banner = bannerMapper.selectByPrimaryKey(Long.parseLong(bannerId));
        if (banner == null) {
            return null;
        }
        BannerInfoOneVo bannerInfoOneVo = new BannerInfoOneVo(banner);
        List<BannerTag> bannerTags = bannerTagService.selectByBannerId(Long.parseLong(bannerId));
        bannerTags.forEach(bannerTag -> bannerInfoOneVo.getTags().add(new BannerTagVo(bannerTag)));
        return bannerInfoOneVo;
    }
    
    @Override
    public List<Banner> getBannerByJumpId(long bannerId) {
        BannerExample bannerExample = new BannerExample();
        BannerExample.Criteria criteria = bannerExample.createCriteria();
        criteria.andJumpIdEqualTo(bannerId);
        return bannerMapper.selectByExample(bannerExample);
    }
    


}
